package com.gufe.controller.admin.club;


import com.gufe.entity.PPTQueryParam;
import com.gufe.entity.PrecollectMember;
import com.gufe.result.PageResult;
import com.gufe.result.Result;
import com.gufe.service.admin.club.ApplyMemberService;
import com.gufe.utils.PdfUtils;
import com.gufe.vo.PPTvo.ApplyMemberVO;
import com.gufe.vo.PPTvo.CollegeCountVO;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/admin/preparatory/apply")
@PreAuthorize("hasRole('ADMIN')")
// 管理端预征班人员管理
public class ApplyMemberController {

    @Autowired
    private ApplyMemberService applyMemberService;

    /**
     *批量同意预征班申请
     * @param ids
     * @return
     */
    @PutMapping()
    public Result agreeApply(@RequestBody List<Integer> ids) {
        log.info("批量同意预征班申请：{}",ids);
        applyMemberService.agreeApply(ids);
        return Result.success("同意成功！！！");
    }

    /**
     * 查询预征班申请成员中各个学院的人数总计
     * @return
     */
    @GetMapping("/members/college")
    public Result getCollegeCount() {
        log.info("查询预征班申请成员各个学院的总人数");
        List<CollegeCountVO> collegeCountVOS = applyMemberService.getCollegeCount();
        return Result.success(collegeCountVOS);
    }


    /**
     * 分页条件查询预征班申请人员
     * @param pptQueryParam
     * @return
     */
    @GetMapping("/members")
    public Result getApplyMember(PPTQueryParam pptQueryParam) {
        log.info("分页条件查询预征班申请人员：{}",pptQueryParam);
        // List<ApplyMemberVO> applyMemberVOS = applyMemberService.getApplyMember(pptQueryParam);
        PageResult<ApplyMemberVO> pageResult = applyMemberService.getApplyMember(pptQueryParam);
        return Result.success(pageResult);
    }


    @GetMapping("/member/exportExcel/{id}")
    public ResponseEntity<byte[]> exportToExcel(@PathVariable Integer id) throws IOException {
        log.info("导出预征班成员ID={} 信息",id);
        XSSFWorkbook workbook = applyMemberService.exportInfo(id);

        // 将 Excel 写入字节流
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        workbook.write(outputStream);
        workbook.close();

        // 设置 HTTP 响应头，让浏览器下载
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Disposition", "attachment; filename*=UTF-8''" +
                URLEncoder.encode("info", StandardCharsets.UTF_8));
        return new ResponseEntity<>(outputStream.toByteArray(), headers, HttpStatus.OK);

    }


    @GetMapping("/member/exportPDF/{id}")
    public void exportToPDF(@PathVariable Integer id, HttpServletRequest request, HttpServletResponse response) {
        PrecollectMember member = applyMemberService.getInfoById(id);
        PdfUtils.generatePdf(request,response,"贵州财经大学预征班学员申请表模版.pdf",member,member.getName()+"预征班报名表");
    }
}
